import 'package:flutter_carplay/flutter_carplay.dart';

class CarplayMain {
  final FlutterCarplay _flutterCarplay = FlutterCarplay();
  late CPListTemplate recommendTabTemplate;
  late CPListTemplate likeTabTemplate;
  late CPListTemplate recentTabTemplate;

  init() {
    //这里创建，因为还没通知平台来展示，所以不占用什么资源，只是一段json
    recommendTabTemplate = createTabTemplate("推荐",
        items: [
          CPListItem(
            text: "Item 1",
            onPress: (complete, self) => print("点击Item 1"),
          ),
          CPListItem(text: "Item 2"),
          CPListItem(text: "Item 3"),
          CPListItem(text: "Item 4"),
        ],
        systemIcon: "play.circle.fill");
    likeTabTemplate = createTabTemplate("我喜欢",
        items: [], systemIcon: "heart.circle.fill", selectThis: (isSelect) {
      if (isSelect) {
        print("选择了我喜欢");
      }
    });
    recentTabTemplate =
        createTabTemplate("最近播放", items: [], systemIcon: "clock.fill");
    FlutterCarplay.addRootTemplate(CPTabBarTemplate(
      title: "carplay",
      templates: [
        //推荐
        recommendTabTemplate,
        //我喜欢
        likeTabTemplate,
        //最近播放
        recentTabTemplate,
      ],
    ));
    _flutterCarplay.addListenerOnConnectionChange(onCarplayConnectionChange);
  }

  ///制作首页tap
  CPListTemplate createTabTemplate(String title,
      {List<CPListItem>? items,
      String? systemIcon,
      Function()? dispose,
      Function(bool isSelect)? selectThis}) {
    String icon = "systemIcon";
    if (systemIcon != null) {
      icon = systemIcon;
    }
    List<CPListSection> sections = [];
    if (items != null && items.isNotEmpty) {
      sections = [
        CPListSection(
          items: items,
        ),
      ];
    }
    return CPListTemplate(
      sections: sections,
      systemIcon: icon,
      title: title,
      emptyViewTitleVariants: ["暂无数据"],
      backButton: CPBarButton(
        title: "返回",
        onPress: () {
          FlutterCarplay.pop(animated: true);
        },
      ),
      dispose: dispose,
      selectThis: selectThis,
    );
  }

  void onCarplayConnectionChange(CPConnectionStatusTypes status) {
    // Do things when carplay state is connected, background or disconnected
    print("@#@#@#flutter   车机状态 " + status.toString());
    //首次启动连接
    if (CPConnectionStatusTypes.connected == status) {
      FlutterCarplay.setRootTemplate(
        rootTemplate: FlutterCarplay.rootTemplate,
        animated: true,
      );
      //刷新
      _flutterCarplay.forceUpdateRootTemplate();
    }
  }

  bool lastFav = false;

  //初始化播放界面
  void initNowPlaying() {
    FlutterCarplay.init(
      template: CPNowPlayingTemplate(
        buttons: [
          //喜欢
          CPNowPlayingButton(
            onPress: () {
              lastFav = !lastFav;
              initNowPlaying();
            },
            image: lastFav
                ? 'images/icon_play_fav_on.png'
                : 'images/icon_play_fav.png',
            type: CPNowPlayingButtonTypes.CPNowPlayingImageButton,
          ),
        ],
        isAlbumArtistButtonEnabled: true,
        nowPlayingTemplateAlbumArtistButtonTapped: () => {
          print(" 点击  AlbumArtist "),
        },
      ),
    );
  }

  void dispose() {
    _flutterCarplay.removeListenerOnConnectionChange();
  }
}
